package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.ems.pojo.IncomeReportVO;
import com.econ.powercloud.ems.pojo.ReportVO;
import com.econ.powercloud.ems.pojo.requestVO.BatchReportRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.ReportRequestVO;
import com.econ.powercloud.ems.service.biz.ElectricBmuReportService;
import com.econ.powercloud.ems.service.biz.ElectricMeterReportService;
import com.econ.powercloud.ems.service.biz.IncomeReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author : zjf
 * @Description : 报表controller
 * @Date : 2021/9/26 20:16
 * @Modified by : zjf
 */
@RestController
@Api(tags ="报表数据查询")
@RequestMapping(value = "/api/v2/report")
public class ReportController extends ApiBaseController {

    @Autowired
    private ElectricMeterReportService electricMeterReportService;

    @Autowired
    private ElectricBmuReportService electricBmuReportService;

    @Autowired
    private IncomeReportService incomeReportService;


    /**
     * 获取电表日报表数据
     */
    @GetMapping("meter/getDataByDay")
    @ApiOperation(value="获取电表日报表数据", notes="获取电表日报表数据")
    public MsgResult<ReportVO> getDataByDay(ReportRequestVO reportRequestVO) {
        return successHandler(electricMeterReportService.getDataByDay(reportRequestVO));
    }

    /**
     * 获取电表月报表数据
     */
    @GetMapping("meter/getDataByMonth")
    @ApiOperation(value="获取电表月报表数据", notes="获取电表月报表数据")
    public MsgResult<ReportVO> getDataByMonth(ReportRequestVO reportRequestVO) {
        return successHandler(electricMeterReportService.getDataByMonth(reportRequestVO));
    }

    /**
     * 获取电表年报表数据
     */
    @GetMapping("meter/getDataByYear")
    @ApiOperation(value="获取电表年报表数据", notes="获取电表年报表数据")
    public MsgResult<ReportVO> getDataByYear(ReportRequestVO reportRequestVO) {
        return successHandler(electricMeterReportService.getDataByYear(reportRequestVO));
    }


    /**
     * 获取BMU日报表数据
     */
    @GetMapping("bmu/getDataByDay")
    @ApiOperation(value="获取BMU日报表数据", notes="获取BMU日报表数据")
    public MsgResult<ReportVO> getBmuDataByDay(ReportRequestVO reportRequestVO) {
        return successHandler(electricBmuReportService.getBmuDataByDay(reportRequestVO));
    }

    @PostMapping("bmu/batch/getDataByDay")
    @ApiOperation(value = "批量获取bms日报数据")
    public MsgResult<ReportVO> exportBmsData(@RequestBody @Validated BatchReportRequestVO batchReportRequestVO) {
        List<String> deviceCodes = batchReportRequestVO.getDeviceCodes();
        ReportVO reportVO = electricBmuReportService.exportData(batchReportRequestVO, deviceCodes);
        return successHandler(reportVO);
    }

    /**
     * 获取储能装置受益报表日报表数据
     */
    @GetMapping("income/getDataByDay")
    @ApiOperation(value="获取储能装置受益报表日报表数据", notes="获取储能装置受益报表日报表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "stationId", value = "储能站id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "equipmentId", value = "储能装置id", dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "reportId", value = "报表类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "起始时间", required = true, dataType = "Long")
    })
    public MsgResult<List<Map<String,String>>> getIncomeDataByDay(@RequestParam("stationId")String stationId,
                                                                  @RequestParam(name = "equipmentId", defaultValue = StringUtils.EMPTY)String equipmentId,
                                                       @RequestParam("reportId")String reportId,@RequestParam("startTime")Long startTime){
        return successHandler(incomeReportService.getEquipmentDayData(stationId, equipmentId, reportId, startTime));
    }


    /**
     * 获取受益报表月报表数据
     */
    @GetMapping("/income/getDataByMonth")
    @ApiOperation(value="获取受益报表月报表数据", notes="获取受益报表月报表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "stationId", value = "储能站id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "equipmentId", value = "储能装置id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "reportId", value = "报表类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "起始时间", required = true, dataType = "Long")
    })
    public MsgResult<IncomeReportVO> getIncomeDataByMonth(@RequestParam("stationId")String stationId,
                                                          @RequestParam(name = "equipmentId", defaultValue = StringUtils.EMPTY)String equipmentId,
                                                          @RequestParam("reportId")String reportId, @RequestParam("startTime")Long startTime){
        return successHandler(incomeReportService.getIncomeDataByMonth(stationId,equipmentId,reportId,startTime));
    }

    /**
     * 获取受益报表年报表数据
     */
    @GetMapping("/income/getDataByYear")
    @ApiOperation(value="获取受益报表年报表数据", notes="获取受益报表年报表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "stationId", value = "储能站id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "equipmentId", value = "储能装置id", dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "reportId", value = "报表类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "起始时间", required = true, dataType = "Long")
    })
    public MsgResult<IncomeReportVO> getIncomeDataByYear(@RequestParam("stationId")String stationId, @RequestParam(name = "equipmentId", defaultValue = StringUtils.EMPTY)String equipmentId,
                                                          @RequestParam("reportId")String reportId, @RequestParam("startTime")Long startTime){
        return successHandler(incomeReportService.getIncomeDataByYear(stationId,equipmentId,reportId,startTime));
    }

    @GetMapping("onOffGrid/getDataByMonth")
    @ApiOperation(value="获取上网/下网电量月报表数据", notes="获取上网/下网电量月报表数据")
    public MsgResult<ReportVO> getOnOffGridDataByMonth(ReportRequestVO reportRequestVO) {
        return successHandler(electricMeterReportService.getOnOffGridDataByMonth(reportRequestVO));
    }

    @GetMapping("onOffGrid/getDataByYear")
    @ApiOperation(value="获取上网/下网电量年报表数据", notes="获取上网/下网电量年报表数据")
    public MsgResult<ReportVO> getOnOffGridDataByYear(ReportRequestVO reportRequestVO) {
        return successHandler(electricMeterReportService.getOnOffGridDataByYear(reportRequestVO));
    }


}
